<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/8/25
  Time: 11:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>科室</title>
    <link href="${pageContext.request.contextPath}/layui/css/layui.css" type="text/css" rel="stylesheet"/>
    <style type="text/css">
        .layui-table-cell{
            height: auto;
            line-height: 28px;
        }

    </style>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.12.4.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/layui/layui.js"></script>
    <script type="text/javascript">
        layui.config({
            version: true //一般用于更新组件缓存，默认不开启。设为true即让浏览器不缓存。也可以设为一个固定的值，如：201610
        });
        //首页展示
        layui.use('table', function(){
            var table = layui.table;
            table.render({
                elem: '#myTable',//一般是id选择器
                url:"department/showPage",//数据接口，请求地址
                page: true,//开启分页
                toolbar:"#myToolbar",//顶部工具栏
                cols: [[ //表头
                    {type:"checkbox",align:"center"},
                    {field: 'departmentid',title:'编号'},
                    {field: 'departmentname', title: '科室名字'},
                    {field: 'departmenttel', title: '科室电话'},
                    {field: 'departmentarea', title: '所在区域'},
                    {title:'操作',toolbar:"#caozuo"}
                ]]
            });
        });
        /*显示弹出层*/
        function openLayer() {
            $("#myForm")[0].reset()
            layui.use('layer', function(){
                var layer = layui.layer;
                //layer.msg('hello');
                layer.open({
                    type:1,
                    content:$("#myForm"),
                    area: ['500px', '300px'],
                    maxmin:true
                })
            });
        }
        //执行添加和修改操作

        layui.use(['layer','form','table'], function() {
            var layer = layui.layer;
            var form = layui.form;
            var table = layui.table;
            form.on("submit(update)",function (data) {
                //console.log(data.field)
                if (data.field.departmentid!=0){
                    //执行修改
                    $.ajax({
                        url:"department/updateDepartment",
                        data:data.field,
                        type:"post",
                        success:function (data) {
                            layer.closeAll('page')
                            if (data.updateResult){
                                layer.alert("修改成功",{time:3000})
                            } else {
                                layer.alert("修改失败",{time:3000})
                            }
                            table.reload("myTable")
                        }
                    })
                } else{
                    //执行添加
                    console.log(data.field)
                    $.ajax({
                        url:"department/adddepartment",
                        data:data.field,
                        type:"post",
                        success:function (data) {

                            layer.closeAll('page')
                            if (data.addResult){
                                layer.alert("添加成功",{time:3000})
                            } else {
                                layer.alert("添加失败",{time:3000})
                            }
                            table.reload("myTable")
                        }
                    })
                }
            })
        })

        /*搜索*/
        function doSeach() {
            //1.获取输入框的内容
            var departmentname = $("#name").val()
            //2.发送请求，并且接受数据
            layui.use('table', function() {
                var table = layui.table;

                //第一个实例
                table.reload('myTable',{
                    where:{"departmentname":departmentname}
                })
            });
        }

        /*删除*/
        layui.use('table', function() {
            var table = layui.table;
            //监听工具条
            table.on('tool(test)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                var tr = obj.tr; //获得当前行 tr 的DOM对象
                //console.log(data.bookid)
                if(layEvent === 'detail'){ //查看
                    //do somehing
                } else if(layEvent === 'del'){ //删除
                    layer.confirm('真的删除行么', function(index){
                        obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                        layer.close(index);
                        //向服务端发送删除指令
                        $.ajax({
                            url:"department/deleteDepartment",
                            data:"id="+data.departmentid,
                            type:"post",
                            success:function (data) {
                                if (data.deletResult){
                                    layer.alert("删除成功",{time:3000})
                                } else {
                                    layer.alert("删除失败",{time:3000})
                                }
                                table.reload("myTable")
                            }
                        })
                    });
                } else if(layEvent === 'edit'){ //编辑
                    //do something
                    openLayer()
                    $.ajax({
                        url:"department/selectOne",
                        data:"id="+data.departmentid,
                        type:"post",
                        success:function (data) {
                            $("#departmentid").prop("value",data.department.departmentid);
                            $("#departmentname").val(data.department.departmentname);
                            $("#departmenttel").val(data.department.departmenttel);
                            $("#departmentarea").val(data.department.departmentarea);
                            //$("#uptime").val(data.book.uptime);
                        }
                    })
                }
            });
        })

        //批量删除
        function doDelete(){
            layui.use(['table','layer'], function() {
                var table = layui.table;
                var layer = layui.layer;
                var checkStatus = table.checkStatus('myTable'); //idTest 即为基础参数 id 对应的值
                //console.log(checkStatus.data) //获取选中行的数据
                //console.log(checkStatus.data.length) //获取选中行数量，可作为是否有选中行的条件
                var students = checkStatus.data;
                console.log(students)
                if (students.length==0){
                    layer.alert("请选择要删除的数据")
                } else {
                    layer.confirm('真的删除行么', function(index) {
                        layer.close(index);
                        //向服务端发送删除指令
                        var ids = Array(students.length);
                        for (var i = 0; i < students.length; i++) {
                            ids[i] = students[i].departmentid
                        }
                        $.ajax({
                            url: "department/deleteSome",
                            data: "ids=" + ids,
                            success: function (data) {
                                layer.alert(data.deleteResult)
                                table.reload("myTable")
                            }
                        })
                    })
                }
            })
        }
    </script>
</head>
<body>
    <table id="myTable" lay-filter="test"></table>

    <div id="myToolbar" style="display: none">
        <button class="layui-btn" type="button" onclick="openLayer()">
            <i class="layui-icon">&#xe608;</i>添加
        </button>
        <button type="button" class="layui-btn layui-btn-radius layui-btn-danger" onclick="doDelete()">
            <i class="layui-icon layui-icon-delete"></i>批量删除
        </button>
        <div class="layui-inline">
            <label class="layui-form-label">科室名</label>
            <div class="layui-input-inline" style="width: 200px;">
                <input type="text" class="layui-input" id="name">
            </div>
            <div class="layui-input-inline" style="width: 100px;">
                <button class="layui-btn layui-btn-normal" type="button" onclick="doSeach()">
                    <i class="layui-icon layui-icon-search"></i>
                </button>
            </div>
        </div>
    </div>

    <div id="caozuo" style="display: none">
        <div class="layui-btn-group">
            <button type="button" class="layui-btn" lay-event="edit">编辑</button>
            <button type="button" class="layui-btn" lay-event="del">删除</button>
        </div>
    </div>
    <%--弹出层--%>
    <form class="layui-form" id="myForm" style="display: none">
        <div class="layui-form-item">
            <label class="layui-form-label">科室名称</label>
            <div class="layui-input-block">
                <input type="text" name="departmentname" id="departmentname" required  lay-verify="required" class="layui-input">
                <input value="0" type="hidden" name="departmentid" class="layui-input" id="departmentid">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">办公室电话</label>
            <div class="layui-input-block">
                <input type="text" id="departmenttel" name="departmenttel" required  lay-verify="required" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所在区域</label>
            <div class="layui-input-block">
                <input type="text" id="departmentarea" name="departmentarea" required  lay-verify="required" class="layui-input">
            </div>
        </div>
        <%--<div class="layui-form-item">
            <label class="layui-form-label">上架时间</label>
            <div class="layui-input-block">
                <input type="text" id="uptime" name="uptime" required  lay-verify="date" class="layui-input">
            </div>
        </div>--%>

        <button class="layui-btn" type="button" lay-submit lay-filter="update">确认</button>
        <button class="layui-btn" type="reset">重置</button>
    </form>
</body>
</html>
